package com.jack.leetcode.eachproblem_2020;

import java.util.ArrayList;
import java.util.List;

public class 子集 {

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        result.add(new ArrayList<>());
        if(nums == null || nums.length == 0){
            return result;
        }
        for(int i = 1;i <= nums.length;i++){
            dfs(result, new ArrayList<>(), nums, 0, i);
        }
        return result;
    }

    private void dfs(List<List<Integer>> result, List<Integer> tmp, int[] nums, int index,
                     int size){
        if(tmp.size() == size){
            result.add(new ArrayList<>(tmp));
            return;
        }
        for(int i = index;i < nums.length;i++){
            tmp.add(nums[i]);
            dfs(result, tmp, nums, i + 1, size);
            tmp.remove(tmp.size() - 1);
        }
    }
}